package com.zzh.takeoutapp.activity;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.VPAdapter;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.utils.NotificationUtils;
import com.zzh.takeoutapp.view.ShadeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @Bind(R.id.viewpager)
    ViewPager mVPager;
    @Bind(R.id.id_indicator_home)
    ShadeView homeShadeView;//首页
    @Bind(R.id.id_indicator_order)
    ShadeView orderShadeView;//订单
    @Bind(R.id.id_indicator_mine)
    ShadeView mineShadeView;//我的

    private List<ShadeView> tabIndicators;
    public VPAdapter mAdapter;
    private NotificationUtils mNotificationUtils;
    private Notification notification;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        tabIndicators = new ArrayList<>();
        tabIndicators.add(homeShadeView);
        tabIndicators.add(orderShadeView);
        tabIndicators.add(mineShadeView);
        homeShadeView.setIconAlpha(1.0f);
        mAdapter = new VPAdapter(getSupportFragmentManager(), this);
        mVPager.addOnPageChangeListener(this);
        mVPager.setAdapter(mAdapter);
        mVPager.setOffscreenPageLimit(tabIndicators.size());

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void setListener() {
        //android8.0及以上使用NotificationUtils
        if (Build.VERSION.SDK_INT >= 26) {
            mNotificationUtils = new NotificationUtils(this);
            Notification.Builder builder2 = mNotificationUtils.getAndroidChannelNotification
                    ("适配android 8限制后台定位功能", "正在后台定位");
            notification = builder2.build();
        } else {
            //获取一个Notification构造器
            Notification.Builder builder = new Notification.Builder(this);
            Intent nfIntent = new Intent(this, MainActivity.class);
            builder.setContentIntent(PendingIntent.
                    getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                    .setContentTitle("适配android 8限制后台定位功能") // 设置下拉列表里的标题
                    .setSmallIcon(R.drawable.ic_logo) // 设置状态栏内的小图标
                    .setContentText("正在后台定位") // 设置上下文内容
                    .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
            notification = builder.build(); // 获取构建好的Notification
        }
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音

    }

    @Override
    public void onAfterInit() {
        String type = getIntent().getType();
        if (type != null) {
            resetTabsStatus();
            if (type.equals("OrderListFragment")) {
                tabIndicators.get(1).setIconAlpha(1.0f);
                mVPager.setCurrentItem(1, false);
            } else if (type.equals("MineFragment")) {
                tabIndicators.get(2).setIconAlpha(1.0f);
                mVPager.setCurrentItem(2, false);
            }
        }
    }

    /**
     * 重置Tab状态
     */
    private void resetTabsStatus() {
        for (int i = 0; i < tabIndicators.size(); i++) {
            tabIndicators.get(i).setIconAlpha(0);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (positionOffset > 0) {
            ShadeView leftTab = tabIndicators.get(position);
            ShadeView rightTab = tabIndicators.get(position + 1);
            leftTab.setIconAlpha(1 - positionOffset);
            rightTab.setIconAlpha(positionOffset);
        }
    }

    @OnClick({R.id.id_indicator_home,
            R.id.id_indicator_order,
            R.id.id_indicator_mine})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_indicator_home:
                resetTabsStatus();
                tabIndicators.get(0).setIconAlpha(1.0f);
                mVPager.setCurrentItem(0, false);
                break;
            case R.id.id_indicator_order:
                resetTabsStatus();
                tabIndicators.get(1).setIconAlpha(1.0f);
                mVPager.setCurrentItem(1, false);
                break;
            case R.id.id_indicator_mine:
                resetTabsStatus();
                tabIndicators.get(2).setIconAlpha(1.0f);
                mVPager.setCurrentItem(2, false);
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 2) {
            tabIndicators.get(position).setIconBitmap(this, R.mipmap.home_navigations_me_click);
        } else {
            tabIndicators.get(2).setIconBitmap(this, R.mipmap.home_navigations_me_notclick);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
